<%@ page contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!-- Search Panel -->
<div class="box search">
	<c:choose>
		<c:when test="${searchType == 'quick'}">
			<h2>
				<spring:message code="quicksearch" />
			</h2>
			<div class="box-content" align="center">
				<form:form action="search" method="GET" commandName="query">
					<form:errors path="*" cssClass="errorblock" element="div" />
					<label><spring:message code="keyword" /></label>
					<form:input path="keyWord" cssClass="field" />
					<input type="submit" class="search-submit"
						value="<spring:message code="search" />" />
					<a href="<c:url value="/search"/>" class="bul"><spring:message
							code="advancesearch" /></a>
					<input type="hidden" name="searchType" value="quick" />
				</form:form>
			</div>
		</c:when>
		<c:otherwise>
			<h2>
				<spring:message code="advancesearch" />
			</h2>
			<form:form action="search" method="GET" commandName="query">
				<form:errors path="*" cssClass="errorblock" element="div" />
				<div class="box-content" align="center">
					<div class="form-items">
						<label><spring:message code="productname" /></label>
						<form:input path="productName" cssClass="field" />
					</div>
					<div class="form-items">
						<label><spring:message code="producttype" /></label>
						<form:select cssClass="field" path="productType"
							items="${productTypes}">
						</form:select>
					</div>
					<div class="form-items">
						<label><spring:message code="producer" /></label>
						<form:select cssClass="field" path="producerName"
							items="${producerNames}">
						</form:select>
					</div>

					<div class="form-items">
						<label><spring:message code="fromprice" /></label>
						<div style="margin-top: 3px;">
							<form:input cssClass="field small-field" path="priceFrom" />
							<label><spring:message code="to" />:</label>
							<form:input cssClass="field small-field" path="priceTo" />
						</div>
					</div>
					<input type="submit" class="search-submit"
						value="<spring:message code="search" />" /><a
						href="<c:url value="/search?searchType=quick"/>" class="bul"><spring:message
							code="quicksearch" /></a><input type="hidden" name="searchType"
						value="advance" />
				</div>
			</form:form>
		</c:otherwise>
	</c:choose>

</div>
<!-- End Search Panel -->

<!-- Search Results -->
<c:if test="${searchResults != null}">
	<!-- Search Results -->
	<div class="box">
		<h2>
			<spring:message code="searchresult" />
			(${searchResults.numItems}
			<spring:message code="result" />
			)
		</h2>
		<div class="products box-content">
			<div class="cl">&nbsp;</div>
			<ul>
				<c:forEach var="result" items="${searchResults.result}">
					<li><a href="<c:url value="/product?id=${result.id}"/>"><img
							class="product-image" src="<c:url value="${result.imagePath}"/>"
							alt="" /></a>
						<div class="product-info">
							<h3>${result.name}</h3>
							<div class="product-desc">
								<h4>${result.type.name}</h4>
								<h4>
									<spring:message code="madein" />
									: ${result.madeIn}
								</h4>
								<h4>
									<spring:message code="producer" />
									: ${result.producer.name}
								</h4>
								<h5>
									<spring:message code="viewed" />
									: ${result.views}
								</h5>
								<strong class="price"> <fmt:formatNumber type="number"
										value="${result.price}" groupingUsed="true" /> VNĐ
								</strong>
							</div>
						</div></li>
				</c:forEach>
			</ul>
			<div class="cl">&nbsp;</div>
		</div>
	</div>



	<c:if test="${fn:length(searchResults.result) > 0}">
		<ul id="pagination-digg">
			<c:choose>
				<c:when test="${searchResults.currentPage == 0}">
					<li class="previous-off">« <spring:message code="previous" /></li>
				</c:when>
				<c:otherwise>
					<li class="previous"><a
						href="<c:url value="/search?${pageURL}&page=${searchResults.currentPage + 1 - 1}"/>">«
							<spring:message code="previous" />
					</a></li>
				</c:otherwise>
			</c:choose>

			<c:choose>
				<c:when test="${searchResults.numPage < 10}">
					<c:set var="firstIndexPage" value="0" scope="request" />
					<c:set var="lastIndexPage" value="${searchResults.numPage - 1}"
						scope="request" />
				</c:when>
				<c:otherwise>
					<c:choose>
						<c:when
							test="${searchResults.currentPage + 4 <= searchResults.numPage - 1 && searchResults.currentPage - 5 >= 0}">
							<c:set var="firstIndexPage"
								value="${searchResults.currentPage - 5}" scope="request" />
							<c:set var="lastIndexPage"
								value="${searchResults.currentPage + 4}" scope="request" />
						</c:when>
						<c:otherwise>
							<c:if test="${searchResults.currentPage - 5 < 0}">
								<c:set var="firstIndexPage" value="0" scope="request" />
								<c:set var="lastIndexPage" value="9" scope="request" />
							</c:if>
							<c:if
								test="${searchResults.currentPage + 4 > searchResults.numPage - 1}">
								<c:set var="firstIndexPage"
									value="${searchResults.numPage - 1 - 9}" scope="request" />
								<c:set var="lastIndexPage" value="${searchResults.numPage - 1}"
									scope="request" />
							</c:if>
						</c:otherwise>
					</c:choose>
				</c:otherwise>
			</c:choose>

			<c:forEach var="index" begin="${firstIndexPage}"
				end="${lastIndexPage}">
				<c:choose>
					<c:when test="${index == searchResults.currentPage}">
						<li class="active">${index + 1}</li>
					</c:when>
					<c:otherwise>
						<li><a
							href="<c:url value="/search?${pageURL}&page=${index + 1}"/>">${index
								+ 1}</a></li>
					</c:otherwise>
				</c:choose>
			</c:forEach>

			<c:choose>
				<c:when
					test="${searchResults.currentPage == (searchResults.numPage-1)}">
					<li class="next-off"><spring:message code="next" /> »</li>
				</c:when>
				<c:otherwise>
					<li class="next"><a
						href="<c:url value="/search?${pageURL}&page=${searchResults.currentPage + 1 + 1}"/>"><spring:message
								code="next" /> »</a></li>
				</c:otherwise>
			</c:choose>

		</ul>
	</c:if>
</c:if>
<!-- End Search Results -->